/**
 * 资产包——立项评估
 * User: zhao shou qiang
 * Date: 2017\01\09
 * Time: 9:32
 */

$(function () {
    //卖方报价
    $('#jdpg1').on('focus','.initiation_bargain',function(){
        var val = $(this).val().replace(/,/g, '');
        $(this).val(val);
    })

    //本金
    $('#jdpg1').on('focus','.package_info_principal',function(){
        var val = $(this).val().replace(/,/g, '');
        $(this).val(val);
    })

    //利息
    $('#jdpg1').on('focus','.package_info_interest',function(){
        var val = $(this).val().replace(/,/g, '');
        $(this).val(val);
    })

    //户数
    $('#jdpg1').on('focus','.package_info_hushu',function(){
        var val = $(this).val().replace(/,/g, '');
        $(this).val(val);
    })


    var package_id = $('#initation_package_id').val(),//资产包id
        initation_base3_7_url = $('#initation_base3_7_url').val(),//立项信息3-7
        initation_transfer_history_url = $('#initation_transfer_history_url').val(),//切换历史记录
        initation_transfer_history_add_url = $('#initation_transfer_history_add_url').val(), //新增流转历史记录
        initation_transfer_history_delete_url = $('#initation_transfer_history_delete_url').val(), //删除流转历史记录
        initation_transfer_history_saves_url = $('#initation_transfer_history_saves_url').val(), //保存多个流转历史记录
        deleteObj;


    //模态框删除确认
    $('.package_investigation').on('click','.investigation_modal_delete',function(){
        var url = $('.package_investigation').find('.model_url').val(),
            id = $('.package_investigation').find('.model_id').val(),
            package_id = $('#initation_package_id').val(), //项目id
            order = $('.package_investigation').find('.model_order').val();

        if (id != '') {
            $.ajax({
                type: "POST",
                url: url,
                data: {id:id, order: order, package_id:package_id},
                datatype: "json",
                success: function (data) {
                    if (data.code == 200) {
                        layer.msg(data.message);
                        deleteObj.parent().parent().remove();
                        $('.package_investigation .modal_close').trigger('click');
                    } else if (data.code == 400) {
                        layer.msg(data.message);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg('系统错误');
                }
            });
        } else {
            layer.msg('删除成功');
            deleteObj.parent().parent().remove();
        }
    });

    //删除流转历史记录
    $('#jdpg1').on('click', '.initation_delete_transfer_history', function () {
        deleteObj = $(this);
        $('.package_investigation').find('.model_url').val(initation_transfer_history_delete_url);
        $('.package_investigation').find('.model_id').val($(this).data('id'));
        var order = $(this).parent().parent().data('order');
        $('.package_investigation').find('.model_order').val(order);
        $("#rouse_modal").trigger("click");
    })

    //删除团队收益分配
    $('#jdpg4').on('click', '.btn_delete', function () {
        deleteObj = $(this);
        $('.package_investigation').find('.model_url').val( $('#initation_income_delete_url').val() );
        $('.package_investigation').find('.model_id').val( $(this).parent().parent().data('id') );
        $('.package_investigation').find('.model_order').val('');
        $("#rouse_modal").trigger("click");
    })

    /*----------------------------------------------------  1、立项信息----------------------------------------------------------*/
    /*-----------  1、基本立项信息  ----------*/
    //点击编辑立项信息
    $('#jdpg1').on('click', '.base_info', function () {
        var edit_state = $('#edit_state').val();
        if (edit_state == 2) {
            layer.msg('有其它立项信息未保存');
            return;
        }
        var initation_base_url = $('#initation_base_url').val(),
            type = 2;
        initation_base_url += '?type=' + type + '&package_id=' + package_id;
        initatin_base_info({}, initation_base_url, type);
    })
    //保存立项信息
    $('#jdpg1').on('click', '.save_initation', function () {
        var initation_base_url = $('#initation_base_url').val(),
            type = 1;
        initation_base_url += '?type=' + type + '&package_id=' + package_id;
        var param = $('#jdpg1').find("form[name='base_info_form']").serialize();
        initatin_base_info(param, initation_base_url, type);
    })
    //取消立项信息
    $('#jdpg1').on('click', '.cancle_initation', function () {
        var initation_base_url = $('#initation_base_url').val(),
            type = 3;
        initation_base_url += '?type=' + type + '&package_id=' + package_id;
        initatin_base_info({}, initation_base_url, type);
    })


    /*-----------  2、流转、前期处置及维护情况  ----------*/
    //点击编辑流转、前期处置及维护情况
    $('#jdpg1').on('click', '.base_transfer_history_edit', function () {
        var edit_state = $('#edit_state').val();
        if (edit_state == 2) {
            layer.msg('有其它立项信息未保存');
            return;
        }
        var url = initation_transfer_history_url,
            type = 1,
            param = {};
        param.type = type;
        param.package_id = package_id;
        initation_transfer_history(param, url, type);
    })
    //取消流转、前期处置及维护情况
    $('#jdpg1').on('click', '.initation_cancel_transfer_history', function () {
        var url = initation_transfer_history_url,
            type = 2,
            param = {};
        param.type = type;
        param.package_id = package_id;
        initation_transfer_history(param, url, type);
    })
    //添加流转信息
    $('#jdpg1').on('click', '.initation_add_transfer_history', function () {
        var url = initation_transfer_history_add_url,
            param = {},
            order = $(this).parent().parent().data('order');

        var html = ' <tr data-order="' + order + '"> ' +
            '<td width="5%"><input type="checkbox" name="ids[]" value="" checked=""></td> ' +
            '<td width="15%"><input type="text" class="transfer_date" name="PackageTransferHistory[transfer_date][]" value=""></td> ' +
            '<td width="15%"><input type="text" class="transfer" name="PackageTransferHistory[transfer][]" value="" placeholder="如：信达—中信"></td> ' +

            /*'<td width="25%"><input type="text" class="contents" name="PackageTransferHistory[content][]" value=""></td> ' +
            '<td width="25%"><input type="text" class="remark" name="PackageTransferHistory[remark][]" value=""></td> ' +*/

            '<td width="25%"><textarea class="contents form-control textarea-edit" style="width: 260px;" name="PackageTransferHistory[content][]" ></textarea></td> ' +
            '<td width="25%"><textarea class="remark form-control textarea-edit" style="width: 260px;" name="PackageTransferHistory[remark][]" ></textarea></td> ' +

            '<input type="hidden" name="PackageTransferHistory[id][]" value=""> ' +
            '<input type="hidden" name="PackageTransferHistory[order][]" value="' + order + '">'+
            '<td class="btn-addpop-td" width="15%"> ' +
            '<button type="button" data-id="" class="btn pull-right btn-pop btn-addpop initation_delete_transfer_history">删除</button> ' +
            '<button type="button" class="btn pull-right btn-add btn-addpop initation_add_transfer_history">添加</button> ' +
            '</td> </tr>';

        $(this).parent().parent().after(html);
    })
    //删除当前流转、前期处置及维护情况
    // $('#jdpg1').on('click', '.initation_delete_transfer_history', function () {
    //     if (confirm('确认删除吗？')) {
    //         var obj = $(this),
    //             id = obj.data('id'),
    //             order = obj.parent().parent().data('order');
    //
    //         if (id>0) {
    //             $.ajax({
    //                 url: initation_transfer_history_delete_url,
    //                 type: 'POST',
    //                 async: true,
    //                 data: {'id': id, 'order': order, 'package_id': package_id},
    //                 //timeout: 5000,    //超时时间
    //                 dataType: 'json',
    //                 success: function (data, textStatus, jqXHR) {
    //                     if (data.code == 200) {
    //                         obj.parent().parent().remove();
    //                     } else if (data.code == 400) {
    //                         layer.msg(data.message);
    //                     }
    //                 },
    //                 error: function (xhr, textStatus) {
    //                     layer.msg('系统错误');
    //                 }
    //             })
    //         } else {
    //             $(this).parent().parent().remove();
    //         }
    //     }
    // })
    //保存当前流转、前期处置及维护情况
    $('#jdpg1').on('click', '.initation_save_transfer_history', function () {
        var param = $('#jdpg1').find('form[name="initation_transfer_history"]').serialize();
        initation_transfer_history_saves_url += '?package_id=' + package_id;
        $.ajax({
            url: initation_transfer_history_saves_url,
            type: 'POST',
            async: true,
            data: param,
            //timeout: 5000,    //超时时间
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                if (data.code == 200) {
                    $('#edit_state').val(1);
                    layer.msg(data.message);
                    $('#jdpg1').find('.base_transfer_history').html(data.data.res);
                    $('#jdpg1').find('.base_transfer_history_edit').show();
                } else if (data.code == 400) {
                    layer.msg(data.message);
                }
            },
            error: function (xhr, textStatus) {
                layer.msg('系统错误');
            }
        })
    })


    /*-----------  立项信息3-7  ----------*/
    //点击编辑立项信息3-7
    $('#jdpg1').on('click', '.edit-img3-7', function () {
        var edit_state = $('#edit_state').val();
        if (edit_state == 2) {
            layer.msg('有其它立项信息未保存');
            return;
        }
        var type = 2;
        var initation_base3_7_url = $('#initation_base3_7_url').val();//立项信息3-7
        initation_base3_7_url += '?package_id=' + package_id + '&type=' + type;
        initation3_7({}, initation_base3_7_url, type);
    })
    //保存立项信息3_7
    $('#jdpg1').on('click', '.save_initation3_7', function () {
        var initation_base3_7_url = $('#initation_base3_7_url').val();//立项信息3-7
        var param = $('#jdpg1').find("form[name='packageInitiation']").serialize(),
            type = 1;
        initation_base3_7_url += '?package_id=' + package_id + '&type=' + type;
        initation3_7(param, initation_base3_7_url, type);
    })
    //取消立项信息3_7
    $('#jdpg1').on('click', '.cancel_initation3_7', function () {
        var initation_base3_7_url = $('#initation_base3_7_url').val();//立项信息3-7
        var param = $('#jdpg1').find("form[name='packageInitiation']").serialize(),
            type = 3;
        initation_base3_7_url += '?package_id=' + package_id + '&type=' + type;
        initation3_7(param, initation_base3_7_url, type);
    })


    /*----------------------------------------------------     4、团队收益分配     ----------------------------------------------------------*/
    //点击编辑团队收益分配
    $('#jdpg4').on('click', '.initation_income_edit', function () {
        var edit_state = $('#edit_state').val();
        if (edit_state == 2) {
            layer.msg('有其它立项信息未保存');
            return;
        }
        var url = $('#initation_income_base_url').val();
        var type = 1;
        url += '?package_id=' + package_id + '&type=' + type;
        initatin_income({}, url, type);
    })
    //取消编辑团队收益分配
    $('#jdpg4').on('click', '.initation_cancel_group_income', function () {
        var url = $('#initation_income_base_url').val();
        var type = 2;
        url += '?package_id=' + package_id + '&type=' + type;
        initatin_income({}, url, type);
    })
    //添加节点
    $('#jdpg4').on('click', '.initation_group_income_add', function () {
        var url = $('#initation_income_delete_url').val();
        var html = '<tr data-id="">' +
            '<input type="hidden" name="PackageInitiationIncome[id][]" value=""/>' +
            '<td><input type="text" name="PackageInitiationIncome[stage][]" placeholder="请输入阶段" value=""></td>' +
            '<td><input type="text" name="PackageInitiationIncome[profit][]" placeholder="请输入成员及分配比例" value=""></td>' +
            '<td class="btn-addpop-td pr20">' +
            '<button type="button" class="btn pull-right btn-pop btn-addpop btn_delete" data-id=""  data-url="' + url + '" ' +
            '>删除 </button>' +
            '</td>' +
            '</tr>';
        $('#jdpg4').find('form[name="initation_group_income_form"]').find('tbody').append(html);
    })
    //删除一条数据
    // $('#jdpg4').on('click', '.btn_delete', function () {
    //     var id = $(this).parent().parent().data('id'),
    //         url = $('#initation_income_delete_url').val();
    //     $('#myModalDelete').find('.model_url').val(url);
    //     $('#myModalDelete').find('.model_id').val(id);
    //     if ($.trim(id) == '') {
    //         $(this).parent().parent().remove();
    //         return false;
    //     }
    //
    //     $('#jdpg4').find('.initation_income_modal').trigger('click');
    //
    //     delete_modal($(this));
    // })
    //模态删除一条数据

    //保存编辑团队收益分配
    $('#jdpg4').on('click', '.initation_saves_group_income', function () {
        var url = $('#initation_income_saves_url').val(),
            param = $('#jdpg4').find('form[name="initation_group_income_form"]').serialize();
        url += '?package_id=' + package_id;

        $.ajax({
            url: url,
            type: 'POST',
            async: true,
            data: param,
            //timeout: 5000,    //超时时间
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                if (data.code == 200) {
                    $('#edit_state').val(1);
                    layer.msg(data.message);
                    $('#jdpg4').find('.initation_income_tab').html(data.data.res);
                    $('#jdpg4').find('.initation_income_edit').show();
                } else if (data.code == 400) {
                    layer.msg(data.message);
                }
            },
            error: function (xhr, textStatus) {
                layer.msg('系统错误');
            }
        })
    })


    /*----------------------------------------------------  5、其他需要说明的事项  ----------------------------------------------------------*/
    //$('#jdpg5').find('textarea').removeAttr('disabled');
    //编辑其他需要说明的事项
    $('#jdpg5').on('click', '.edit-img', function () {
        var edit_state = $('#edit_state').val();
        if (edit_state == 2) {
            layer.msg('有其它立项信息未保存');
            return;
        }
        var other_id = $('#other_initiation_id').val(),
            type = 3,
            initation_base_other_url = $('#initation_base_other_url').val();//其它立项信息
        initation_base_other_url += '?type=' + type + '&other_id=' + other_id;

        initation_other({}, initation_base_other_url, type, $(this));
    })
    //保存其他需要说明的事项
    $('#jdpg5').on('click', '.save_initation_other', function () {
        var param = $('#jdpg5').find("form[name='packageInitiationOther']").serialize(),
            type = 1,
            initation_base_other_url = $('#initation_base_other_url').val();//其它立项信息
        initation_base_other_url += '?type=' + type;
        initation_other(param, initation_base_other_url, type, {});
    })
    //取消其他需要说明的事项
    $('#jdpg5').on('click', '.cancel_initation_other', function () {
        var param = $('#jdpg5').find("form[name='packageInitiationOther']").serialize(),
            type = 2,
            initation_base_other_url = $('#initation_base_other_url').val();//其它立项信息
        initation_base_other_url += '?type=' + type;
        initation_other(param, initation_base_other_url, type, {});
    })


    /*----------------------------------------------------   6、报价信息   ----------------------------------------------------------*/
    //切换报价信息
    $('#jdpg6').on('click', '.initiation_price_detail_edit', function () {
        var edit_state = $('#edit_state').val();
        if (edit_state == 2) {
            layer.msg('有其它立项信息未保存');
            return;
        }
        var url = $('#initiation_price_detail').val(),
            type = 2;
        url += '?type=' + type + '&package_id=' + package_id;
        initiation_price_detail({}, url, type);
    })
    //编辑报价信息
    $('#jdpg6').on('click', '.save_price_detail', function () {
        var url = $('#initiation_price_detail').val(),
            type = 1;
        url += '?type=' + type + '&package_id=' + package_id;
        var param = $('#jdpg6').find("form[name='initiation_price_detail']").serialize();
        initiation_price_detail(param, url, type);
    })
    //取消编辑
    $('#jdpg6').on('click', '.cancel_price_detail', function () {
        var url = $('#initiation_price_detail').val(),
            type = 3;
        url += '?type=' + type + '&package_id=' + package_id;
        //$('#jdpg6').find('.initiation_price_detail_edit').show();
        initiation_price_detail({}, url, type);
    })
    //initiation_price_detail
})

function delete_modal( obj) {
    $('#jdpg4').on('click', '.initation_modal_delete', function () {
        var url = $(this).parent().find('.model_url').val();
        var id = $(this).parent().find('.model_id').val();
        var package_id = $('#initation_package_id').val();

        $.ajax({
            url: url,
            type: 'POST',
            async: true,
            data: {'id': id, 'package_id': package_id},
            //timeout: 5000,    //超时时间
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                if (data.code == 200) {
                    $('#jdpg4').find('.modal_close').trigger('click');
                    layer.msg(data.message);
                    obj.parent().parent().remove();
                    //$('#jdpg4').find('.initation_income_tab').html(data.data.res);
                    //$('#jdpg4').find('.initation_income_edit').show();
                } else if (data.code == 400) {
                    layer.msg(data.message);
                }
            },
            error: function (xhr, textStatus) {
                layer.msg('系统错误');
            }
        })
    })
}

/**
 * 报价信息
 * @param param 参数
 * @param url 地址
 * @param type 类型
 */
function initiation_price_detail(param, url, type) {
    $.ajax({
        url: url,
        type: 'POST',
        async: true,
        data: param,
        //timeout: 5000,    //超时时间
        dataType: 'json',
        success: function (data, textStatus, jqXHR) {
            if (data.code == 200) {
                if (type == 1) {
                    $('#edit_state').val(1);
                    $('#jdpg6').find('.initiation_price_detail').html(data.data.res);
                    $('#jdpg6').find('.initiation_price_detail_edit').show();
                } else if (type == 2) {
                    $('#edit_state').val(2);
                    $('#jdpg6').find('.initiation_price_detail').html(data.data.res);
                    $('#jdpg6').find('.initiation_price_detail_edit').hide();
                } else if (type == 3) {
                    $('#edit_state').val(1);
                    $('#jdpg6').find('.initiation_price_detail').html(data.data.res);
                    $('#jdpg6').find('.initiation_price_detail_edit').show();
                } else {
                    $('#edit_state').val(1);
                }
            } else if (data.code == 400) {
                layer.msg(data.message);
            }
        },
        error: function (xhr, textStatus) {
            layer.msg('系统错误');
        }
    })
}

/**
 * 团队收益分配
 * @param param 参数
 * @param url 地址
 * @param type 类型
 */
function initatin_income(param, url, type) {
    $.ajax({
        url: url,
        type: 'POST',
        async: true,
        data: param,
        //timeout: 5000,    //超时时间
        dataType: 'json',
        success: function (data, textStatus, jqXHR) {
            if (data.code == 200) {
                if (type == 1) {
                    $('#edit_state').val(2);
                    $('#jdpg4').find('.initation_income_tab').html(data.data.res);
                    $('#jdpg4').find('.initation_income_edit').hide();
                } else if (type == 2) {
                    $('#edit_state').val(1);
                    $('#jdpg4').find('.initation_income_tab').html(data.data.res);
                    $('#jdpg4').find('.initation_income_edit').show();
                } else {
                    $('#edit_state').val(1);
                }
            } else if (data.code == 400) {
                layer.msg(data.message);
            }
        },
        error: function (xhr, textStatus) {
            layer.msg('系统错误');
        }
    })
}

/**
 * 流转、前期处置及维护情况
 * @param param 参数
 * @param url 地址
 */
function initation_transfer_history(param, url, type) {
    $.ajax({
        url: url,
        type: 'POST',
        async: true,
        data: param,
        //timeout: 5000,    //超时时间
        dataType: 'json',
        success: function (data, textStatus, jqXHR) {
            if (data.code == 200) {
                if (type == 1) {
                    $('#edit_state').val(2);
                    $('#jdpg1').find('.base_transfer_history').html(data.data.res);
                    $('#jdpg1').find('.base_transfer_history_edit').hide();
                } else if (type == 2 || type == 3) {
                    if (type == 2) {
                        $('#edit_state').val(1);
                    }
                    $('#jdpg1').find('.base_transfer_history').html(data.data.res);
                    $('#jdpg1').find('.base_transfer_history_edit').show();
                } else {
                    $('#edit_state').val(1);
                }
            } else if (data.code == 400) {
                layer.msg(data.message);
            }
        },
        error: function (xhr, textStatus) {
            layer.msg('系统错误');
        }
    })
}

/**
 * 其他需要说明的事项
 * @param param 参数
 * @param url 地址
 * @param type 类型 1、修改，2、只读，3、取消返回
 */
function initation_other(param, url, type, obj) {
    $.ajax({
        url: url,
        type: 'POST',
        data: param,
        dataType: 'json',
        success: function (data, textStatus, jqXHR) {
            if (data.code == 200) {
                if (type == 3) {
                    $('#edit_state').val(2);
                    obj.hide();
                    $('#jdpg5').find('textarea').removeAttr('disabled');
                    $('#jdpg5').find('.btns').show();
                } else {
                    $('#edit_state').val(1);
                    if (type == 1) {
                        layer.msg(data.message);
                    }
                    $('#jdpg5').find('.edit-img').show();
                    $('#jdpg5').find('textarea').val(data.data.res.other_notes);
                    $('#jdpg5').find('textarea').attr('disabled', true);
                    $('#jdpg5').find('.btns').hide();
                }
            } else if (data.code == 400) {
                layer.msg(data.message);
            }
        },
        error: function (xhr, textStatus) {
            layer.msg('系统错误');
        }
    })
}

/**
 * 立项信息3-7
 * @param param 参数
 * @param url 地址
 */
function initation3_7(param, url, type) {
    $.ajax({
        url: url,
        type: 'POST',
        async: true,
        data: param,
        //timeout: 5000,    //超时时间
        dataType: 'json',
        success: function (data, textStatus, jqXHR) {
            if (data.code == 200) {
                $('#jdpg1').find('.edit-img3-7').show();
                if (type == 1) {
                    $('#edit_state').val(1);
                    layer.msg(data.message);
                } else if (type == 2) {
                    $('#jdpg1').find('.edit-img3-7').hide();
                    $('#edit_state').val(2);
                } else if (type == 3) {
                    $('#edit_state').val(1);
                } else {
                    $('#edit_state').val(1);
                }
                $('#jdpg1').find('.package_initation3_7').html(data.data.res);

                //验证数字
                $.validator.setDefaults({
                    debug: true
                })
                $("#packageInitiationEdit").validate({});

            } else if (data.code == 400) {
                layer.msg(data.message);
            }
        },
        error: function (xhr, textStatus) {
            layer.msg('系统错误');
        }
    })
}

/**
 * 立项信息
 * @param param 参数
 * @param url 地址
 */
function initatin_base_info(param, url, type) {
    $.ajax({
        url: url,
        type: 'POST',
        async: true,
        data: param,
        //timeout: 5000,    //超时时间
        dataType: 'json',
        success: function (data, textStatus, jqXHR) {
            if (data.code == 200) {
                $('#jdpg1').find('.base_info').show();
                if (type == 1) {
                    $('#edit_state').val(1);
                    layer.msg(data.message);
                } else if (type == 2) {
                    $('#jdpg1').find('.base_info').hide();
                    $('#edit_state').val(2);
                } else if (type == 3) {
                    $('#edit_state').val(1);
                } else {
                    $('#edit_state').val(1);
                }
                $('#jdpg1').find('.base_info_toggle').html(data.data.res);

                //验证本金利息
                $.validator.setDefaults({
                    debug: true
                })
                $("#base_info_form_edit").validate({});

            } else if (data.code == 400) {
                layer.msg(data.message);
            }
        },
        error: function (xhr, textStatus) {
            layer.msg('系统错误');
        }
    })
}

//切换资产包类型
function type_select(type) {
    $('#package_type').val(type);
}
